core: Use O_NOATIME to open metadata
authorColin Walters <walters@verbum.org>
Mon, 27 Aug 2012 20:07:39 +0000 (16:07 -0400)
committerColin Walters <walters@verbum.org>
Mon, 27 Aug 2012 20:07:39 +0000 (16:07 -0400)
We really don't need atime for metadata, it's just a speed hit.

src/libostree/ostree-repo.c
src/libotutil/ot-unix-utils.c
src/libotutil/ot-unix-utils.h
src/libotutil/ot-variant-utils.c

index ab41e30dabe79ca992ef9201a74c4ec696bc56d2..1503dd7c19b58499ee9bf528b24276555f11f862 100644 (file)
@@ -812,12 +812,8 @@ ensure_file_data_synced (GFile         *file,
   gboolean ret = FALSE;
   int fd = -1;
 
-  fd = g_open (ot_gfile_get_path_cached (file), O_RDONLY | O_NOATIME | O_CLOEXEC | O_LARGEFILE, 0);
-  if (fd < 0)
-    {
-      ot_util_set_error_from_errno (error, errno);
-      goto out;
-    }
+  if (!ot_unix_open_noatime (ot_gfile_get_path_cached (file), &fd, error))
+    goto out;
 
   if (!ot_unix_fdatasync (fd, error))
     goto out;
index 600082d6ebab2e02726c52e2249c379b13d9ed82..3b0645f9a9786cafe28386136e0a6f8f952fe2ce 100644 (file)
 
 #include "config.h"
 
+#define _GNU_SOURCE
+
 #include "otutil.h"
 
 #include <gio/gio.h>
+#include <glib/gstdio.h>
 #include <gio/gunixoutputstream.h>
 
 #include <string.h>
 #include <sys/types.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -225,3 +229,29 @@ ot_unix_close (int fd, GError **error)
     }
   return TRUE;
 }
+
+/**
+ * ot_unix_open_noatime:
+ *
+ * Open a file for reading, using O_NOATIME if possible.
+ */
+gboolean
+ot_unix_open_noatime (const char    *path,
+                      int           *out_fd,
+                      GError       **error)
+{
+  int fd;
+
+#ifdef O_NOATIME
+  fd = g_open (path, O_RDONLY | O_NOATIME | O_CLOEXEC, 0);
+  if (fd == -1 && errno == EPERM)
+#endif
+    fd = g_open (path, O_RDONLY | O_CLOEXEC, 0);
+  if (fd == -1)
+    {
+      ot_util_set_error_from_errno (error, errno);
+      return FALSE;
+    }
+  *out_fd = fd;
+  return TRUE;
+}
index 2022dea89f070ea2e3229ab50266590f52584f9e..18f07494e76fb0ff2edfdb8852b9314eb8d7e16a 100644 (file)
@@ -59,6 +59,8 @@ gboolean ot_unix_fdatasync (int fd, GError **error);
 
 gboolean ot_unix_close (int fd, GError **error);
 
+gboolean ot_unix_open_noatime (const char *path, int *out_fd, GError **error);
+
 G_END_DECLS
 
 #endif
index 93b69b846742b9566342abc5a74c568281231f48..973dbf16eb98d9ebc9c4cf52c90dbc9a43e0a016 100644 (file)
@@ -118,11 +118,16 @@ ot_util_variant_map (GFile              *src,
   const char *path = NULL;
   ot_lvariant GVariant *ret_variant = NULL;
   GMappedFile *mfile = NULL;
+  int fd;
 
   path = ot_gfile_get_path_cached (src);
-  mfile = g_mapped_file_new (path, FALSE, error);
+  if (!ot_unix_open_noatime (path, &fd, error))
+    goto out;
+  mfile = g_mapped_file_new_from_fd (fd, FALSE, error);
   if (!mfile)
     goto out;
+  if (!ot_unix_close (fd, error))
+    goto out;
 
   ret_variant = g_variant_new_from_data (type,
                                          g_mapped_file_get_contents (mfile),